package models;

import play.Logger;

public class StateManager
{
    public StateManager.GameState state;

    public StateManager()
    {
        this.state = StateManager.GameState.WAITING;
    }

    public boolean canJoin()
    {
        return this.state.equals(StateManager.GameState.WAITING);
    }

    public boolean canBet()
    {
        return this.state.equals(StateManager.GameState.BETS);
    }

    public boolean canLicitate()
    {
        return this.state.equals(StateManager.GameState.LICITATION1)
                || this.state.equals(StateManager.GameState.LICITATION2);
    }

    public boolean canExchange()
    {
        return this.state.equals(StateManager.GameState.EXCHANGING);
    }

    public boolean isFinished()
    {
        return this.state.equals(StateManager.GameState.END);
    }

    public void nextState()
    {
        GameState old = this.state;
        switch (this.state)
        {
            case WAITING:
                this.state = StateManager.GameState.BETS;
                break;
            case BETS:
                this.state = StateManager.GameState.LICITATION1;
                break;
            case LICITATION1:
                this.state = StateManager.GameState.EXCHANGING;
                break;
            case EXCHANGING:
                this.state = StateManager.GameState.LICITATION2;
                break;
            case LICITATION2:
                this.state = StateManager.GameState.END;
                break;
            case END:
                this.state = StateManager.GameState.BETS;
                break;
            default:
                // exception?
                break;
        }
        Logger.debug("[ StateManager] Game state changed from " + old + " to "
                + this.state + ".");
    }

    public GameState current()
    {
        return this.state;
    }

    public static void main(String[] args)
    {
        StateManager sm = new StateManager();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);
        sm.nextState();
        System.out.println(sm.state);

    }

    public static enum GameState
    {
        WAITING, BETS, LICITATION1, EXCHANGING, LICITATION2, END;

        public int toInteger()
        {
            switch (this)
            {
                case WAITING:
                    return 0;
                case BETS:
                    return 1;
                case LICITATION1:
                    return 2;
                case EXCHANGING:
                    return 3;
                case LICITATION2:
                    return 4;
                case END:
                    return 5;
                default:
                    return -1;
            }
        }
    }

}
